package com.ruoyi.worksite.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.worksite.domain.vo.SysMacRoleVo;
import com.ruoyi.worksite.domain.bo.SysMacRoleBo;
import com.ruoyi.worksite.service.ISysMacRoleService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * MAC地址验证
 *
 * @author ruoyi
 * @date 2023-07-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/worksite/macRole")
public class SysMacRoleController extends BaseController {

    private final ISysMacRoleService iSysMacRoleService;

    /**
     * 查询MAC地址验证列表
     */
    @SaCheckPermission("worksite:macRole:list")
    @GetMapping("/list")
    public TableDataInfo<SysMacRoleVo> list(SysMacRoleBo bo, PageQuery pageQuery) {
        return iSysMacRoleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出MAC地址验证列表
     */
    @SaCheckPermission("worksite:macRole:export")
    @Log(title = "MAC地址验证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysMacRoleBo bo, HttpServletResponse response) {
        List<SysMacRoleVo> list = iSysMacRoleService.queryList(bo);
        ExcelUtil.exportExcel(list, "MAC地址验证", SysMacRoleVo.class, response);
    }

    /**
     * 获取MAC地址验证详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("worksite:macRole:query")
    @GetMapping("/{id}")
    public R<SysMacRoleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iSysMacRoleService.queryById(id));
    }

    /**
     * 新增MAC地址验证
     */
    @SaCheckPermission("worksite:macRole:add")
    @Log(title = "MAC地址验证", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysMacRoleBo bo) {
        return toAjax(iSysMacRoleService.insertByBo(bo));
    }

    /**
     * 修改MAC地址验证
     */
    @SaCheckPermission("worksite:macRole:edit")
    @Log(title = "MAC地址验证", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysMacRoleBo bo) {
        return toAjax(iSysMacRoleService.updateByBo(bo));
    }

    /**
     * 删除MAC地址验证
     *
     * @param ids 主键串
     */
    @SaCheckPermission("worksite:macRole:remove")
    @Log(title = "MAC地址验证", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iSysMacRoleService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
    /**
     * 验证MAC地址是否合法
     *
     * @param macAddr MAC地址
     */
    @GetMapping("/verify/{macAddr}")
    public R<String> macIsOk(@NotNull(message = "主键不能为空")
                             @PathVariable String macAddr) {
        // MAC 地址的正则表达式
        String macRegex = "^([0-9a-fA-F]{2}(:|-)){5}[0-9a-fA-F]{2}$";
        // 编译正则表达式
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(macRegex);
        // 使用正则表达式进行匹配
        if ( !pattern.matcher(macAddr).matches()){
            return R.fail("MAC地址不合法");
        }
        Boolean verify = iSysMacRoleService.verify(macAddr);
        if (verify){
            return R.ok("验证成功");
        }else {
            return R.fail("验证失败");
        }
    }
}
